<?php

namespace Biz\service\Album;

use Biz\cache\album\AlbumRecommendCache;
use Biz\cache\album\AlbumSquarePool;
use Biz\model\Album\AlbumListModel;
use Doraemon\model\McnUserExtendInfo;
use Doraemon\service\Follow;
use Doraemon\service\User;
use Doraemon\tools\HhzClient;
use Doraemon\tools\HhzEnv;
use hhz\constant\redis\redisKeys;
use Hhz\Redis\newCache;

class AlbumRecommendService
{
    public static function list(): array
    {

        $newRedis = new newCache(redisKeys::KEY_B_CACHE_ALBUM_RECOMMEND_OFFICIAL_LIST);
        $aData = $newRedis->getCache();
        $albumIds = $aData ? explode(',', $aData):[];

        if ($albumIds) {
            $albumList = AlbumListModel::selectByWhere(['album_id' => $albumIds]);
            $albumMap = array_column($albumList, null, 'album_id');
            foreach ($albumIds as $albumId) {
                $value['title'] = $albumMap[$albumId]['title'] ?? '';
                $value['desc'] = $albumMap[$albumId]['desc'] ?? '';
                $value['cover_img'] = $albumMap[$albumId]['cover_img'] ?? '';
                $value['tags'] = $albumMap[$albumId]['tags'] ?? '';
                $value['status'] = AlbumListModel::STATUS_MAP[$albumMap[$albumId]['status']] ?? '';
                $value['uid'] = $albumMap[$albumId]['uid'] ?? '';
                $value['album_id'] = $albumId;
                $list['list'][] = $value;
            }
        }
        $list['total'] = count($albumIds);

        return $list;
    }

    /**
     * 清单广场推荐清单
     * @return void
     */
    public static function squareRecommend()
    {
        $newRedis = new newCache(redisKeys::KEY_B_CACHE_ALBUM_SQUARE_RECOMMEND_POOL);
        $albumIds = $newRedis->getCache();
        $albumIds = $albumIds ? explode(',', $albumIds) : [];

        $albumIds = array_slice($albumIds, 0, 20);
        $res = AlbumListModel::selectByWhere([
            'status' => AlbumListModel::STATS_LIST_PUBLIC_SHOW_MAP,
            'quality' => [AlbumListModel::QUALITY_GOOD, AlbumListModel::QUALITY_EXCELLENT],
            'album_id' => $albumIds
        ]);
        $list = FormatAlbumService::_formatAlbumList($res, FormatAlbumService::SQUARE_LIST);
        $count = count($list);
        $maxNum = 6;
        $num = ($count > $maxNum) ? $maxNum : $count;
        $randKey = array_rand($list, $num);
        $result = [];
        foreach ($randKey as $key) {
            $result[] = $list[$key];
        }
        return $result;
    }

    /**
     * 清单广场达人清单
     * @return void
     */
    public static function darenRecommend()
    {
        $newRedis = new newCache(redisKeys::KEY_B_CACHE_ALBUM_SQUARE_DAREN_POOL);
        $uids = $newRedis->getCache();
        $uids = $uids ? explode(',', $uids) : [];
        if (HhzEnv::isShopTest()) {//debug 开发环境debug
            $uids = [7323022, 11941];
        }
        shuffle($uids);
        $uids = array_slice($uids, 0, 10);//最后取4个
        $mcnInfos = McnUserExtendInfo::selectByWhere([
            'uid' => $uids,
            'personal_preference[!]' => ''
        ], ['uid', 'personal_preference']);
        $recommendationArr = [];
        foreach ($mcnInfos as $val) {
            $personal_tags = explode(",", $val['personal_preference']);
            $recommendationArr[$val['uid']] = $personal_tags[0] ?? '';
        }
        $res = AlbumListModel::selectByWhere([
            'uid' => $uids,
            'status' => AlbumListModel::STATS_LIST_PUBLIC_SHOW_MAP,
            'quality' => [AlbumListModel::QUALITY_GOOD, AlbumListModel::QUALITY_EXCELLENT],
            'ORDER' => ['publish_time' => 'DESC'],
        ]);
        $userInfoMap = User::getBatchBasicUserInfo($uids);
        $userInfoMap = array_column($userInfoMap, null, 'uid');
        $list = FormatAlbumService::_formatAlbumList($res, FormatAlbumService::SQUARE_LIST);
        $recommendList = [];
        foreach ($list as $row) {
            $uid = $row['album_info']['uid'];
            if (!isset($recommendList[$uid])) {
                $userInfo = $userInfoMap[$uid];
                $newFollowStatus = Follow::FollowStatus(HhzClient::getUid(), $uid);
                $userInfo['is_follow_new'] = $newFollowStatus[$uid] ?? 0;
                $recommendation = $recommendationArr[$uid] ?? '';
                $recommendation = ($recommendation == 'null') ? '' : $recommendation;
                $recommendList[$uid] = [
                    'user_info' => [
                        'uid' => $userInfo['uid'],
                        'nick' => $userInfo['nick'],
                        'avatar' => $userInfo['avatar'],
                        'small_avatar' => $userInfo['avatar'],
                        'recommendation' => $recommendation,
                        'is_follow_new' => $userInfo['is_follow_new']
                    ]
                ];
            }
            if (!isset($recommendList[$uid]['album_list']) || count($recommendList[$uid]['album_list']) < 3) {
                $recommendList[$uid]['album_list'][] = $row;
            }
        }

        $result = [];
        foreach ($uids as $uid) {
            if (isset($recommendList[$uid]['album_list']) && count($recommendList[$uid]['album_list']) >= 3) {
                $result[] = $recommendList[$uid];
            }
        }
        $result = array_slice($result, 0, 4);
        return $result;
    }

}